package MuleBackend;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

import MuleBackend.Stages.TimeClass;

public class TownPanel extends JPanel{
	
	long l;
	TimeClass tc;
	Player p;
	Inventory inventory;
	
	private JLabel crystite, smithore, energy, food, mule, pub,landSale,asset;
	private JLabel waterWay1,waterWay2, landWay;
	private JLabel crystitePrice, smithorePrice, energyPrice, foodPrice, mulePrice;
	
	private final ImageIcon WATER = new ImageIcon("src/images/water1.png");
	private final ImageIcon FOOD = new ImageIcon("src/images/foodBooth1.png");
	private final ImageIcon ENERGY = new ImageIcon("src/images/energyBooth1.png");
	private final ImageIcon SMITHORE = new ImageIcon("src/images/smithoreBooth.png");
	private final ImageIcon CRYSTITE = new ImageIcon("src/images/crystiteBooth.png");
	private final ImageIcon MULE = new ImageIcon("src/images/muleBooth.png");
	private final ImageIcon PUB = new ImageIcon("src/images/pubBooth.png");
	private final ImageIcon LANDSALE = new ImageIcon("src/images/saleBooth.png");
	private final ImageIcon ASSET = new ImageIcon("src/images/AssayBooth.png");
	private final ImageIcon LANDWAY = new ImageIcon("src/images/landWay.png");
	
	public int x11,y11,speed,muleX,muleY;
	public Player player;
//	public PlayerControl playerControl;
	public boolean hasMule;
	
	public TownPanel(){
		setLayout(null);
		player = null;
		hasMule = false;
		x11 = 10;
		y11=300;
		speed = 15;
		buildGUI();
	}
	
	private void buildGUI(){
		
		crystite = new JLabel(CRYSTITE);
		crystite.setBounds(0,0,200,200);
		crystite.setBorder(new LineBorder(Color.BLACK));
		smithore = new JLabel(SMITHORE);
		smithore.setBounds(200,0,200,200);
		smithore.setBorder(new LineBorder(Color.BLACK));
		energy = new JLabel(ENERGY);
		energy.setBounds(500,0,200,200);
		energy.setBorder(new LineBorder(Color.BLACK));
		food = new JLabel(FOOD);
		food.setBounds(700,0,200,200);
		food.setBorder(new LineBorder(Color.BLACK));
		waterWay1 = new JLabel(WATER);
		waterWay1.setBounds(400,0,100,200);
		waterWay1.setBorder(new LineBorder(Color.BLACK));
		
		asset = new JLabel(ASSET);
		asset.setBounds(0,300,200,200);
		asset.setBorder(new LineBorder(Color.BLACK));
		landSale = new JLabel(LANDSALE);
		landSale.setBounds(200,300,200,200);
		landSale.setBorder(new LineBorder(Color.BLACK));
		pub = new JLabel(PUB);
		pub.setBounds(500,300,200,200);
		pub.setBorder(new LineBorder(Color.BLACK));
		mule = new JLabel(MULE);
		mule.setBounds(700,300,200,200);
		mule.setBorder(new LineBorder(Color.BLACK));
		waterWay2 = new JLabel(WATER);
		waterWay2.setBounds(400,300,100,200);
		waterWay2.setBorder(new LineBorder(Color.BLACK));
		
		landWay = new JLabel(LANDWAY);
		landWay.setBounds(0,200,900,100);
		landWay.setBorder(new LineBorder(Color.BLACK));
		crystitePrice = new JLabel("$100");
		crystitePrice.setHorizontalAlignment(SwingConstants.CENTER);
		crystitePrice.setFont(new Font("Tahoma", Font.PLAIN, 24));
		crystitePrice.setBounds(50,0,91,25);
		smithorePrice = new JLabel("$50");
		smithorePrice.setHorizontalAlignment(SwingConstants.CENTER);
		smithorePrice.setFont(new Font("Tahoma", Font.PLAIN, 24));
		smithorePrice.setBounds(250,0,85,25);
		energyPrice = new JLabel("$25");
		energyPrice.setHorizontalAlignment(SwingConstants.CENTER);
		energyPrice.setFont(new Font("Tahoma", Font.PLAIN, 24));
		energyPrice.setBounds(550,0,85,25);
		foodPrice = new JLabel("$30");
		foodPrice.setHorizontalAlignment(SwingConstants.CENTER);
		foodPrice.setFont(new Font("Tahoma", Font.PLAIN, 24));
		foodPrice.setBounds(755,0,85,25);
		
		mulePrice = new JLabel("$100");
		mulePrice.setHorizontalAlignment(SwingConstants.CENTER);
		mulePrice.setFont(new Font("Tahoma", Font.PLAIN, 24));
		mulePrice.setBounds(755, 475, 85, 25);

		add(crystitePrice);
		add(smithorePrice);
		add(energyPrice);
		add(foodPrice);
		add(mulePrice);
		add(crystite);
		add(smithore);
		add(energy);
		add(food);
		add(waterWay1);
		add(landWay);
		add(asset);
		add(landSale);
		add(pub);
		add(mule);
		add(waterWay2);
	}
	
	public void setMule(boolean hasMule){this.hasMule = hasMule;}
	
	/**
	 * the location where player start
	 * @param x the coordinate on x-axis
	 * @param y the coordinate on y-axis
	 */
	public void startPlayer(int x, int y){
		this.x11 = x;
		this.y11 = y;
	}
	
	/**
	 * this method performed task when the player enter 
	 * different booth
	 */
	public void doStuff(int x, int y){
		if (x<195 && y>=400 && player.getMule()==0){
			player.setMule(6);
			System.out.println("this is asset");
		}
		if (x>=195 && x<=400 && y>=400 && player.getMule()==0){
			player.setMule(7);
			System.out.println("this is land sale");
		}
		if (x>=495 && x<695 && y>=400){
			System.out.println("this is pub");			   
			   //calculate how much money the player gets
	     		gamble(player);
	     		
	     		System.out.println("Player" + player + 
	    		 "'s current money: "+ player.getMoney());
	     		
	     		//stop the turn
//	 	   		GameDriver.game.boardGUI.endTurn();
	 	   		GameDriver.game.endTimer();
	     }
		if (x>=695 && y>=400 && !hasMule && player.getMule()==0){
			System.out.println("this is mule");
			if (player.hasMoney(Town.MULE_PRICE)){
				player.deductMoney(Town.MULE_PRICE);
				GameDriver.game.getTown().setMuleStock(-1);
				hasMule = true;
				player.setMule(1);
			}
		}
		if (x<195 && y<=100 && hasMule){
			System.out.println("this is crystite");
			if (player.hasMoney(Town.CRYSTITE_FIT_PRICE)){
				player.deductMoney(Town.CRYSTITE_FIT_PRICE);
				hasMule = false;
				player.setMule(5);
			}
		}
		if (x>=195 && x<=400 && y<=100 && hasMule){
			System.out.println("this is smithore");
			if (player.hasMoney(Town.SMITHORE_FIT_PRICE)){
				player.deductMoney(Town.SMITHORE_FIT_PRICE);
				hasMule = false;
				player.setMule(4);
			}
			
		}
		if (x>=495 && x<695 && y<=100 && hasMule){
			System.out.println("this is energy");
			if (player.hasMoney(Town.ENERGY_FIT_PRICE)){
				player.deductMoney(Town.ENERGY_FIT_PRICE);
				hasMule = false;
				player.setMule(3);
			}
		}
		if (x>=695 && y<=100 && hasMule){
			System.out.println("this is food");
			if (player.hasMoney(Town.FOOD_FIT_PRICE)){
				player.deductMoney(Town.FOOD_FIT_PRICE);
				hasMule = false;
				player.setMule(2);
			}
		}
		GameDriver.game.updateInfoPanels();
	}

	private void gamble(Player player) {
		
 	   	   Player activePlayer = player;

	   	   int roundNumber = GameDriver.game.getRoundNumber();	
	   	   int roundBonus = 0;
 	   	   int timeBonus = 0;
 	   	   int time;
 	   System.out.println(activePlayer.toString());
 	   	
 	   				//Calculate the correct round bonus to be given:
 	   			   {
 	   				   if(roundNumber >= 1 && roundNumber <= 3)
 	   					   roundBonus = 50;
 	   	
 	   				   else if(roundNumber >= 4 && roundNumber <= 7)
 	   					   roundBonus = 100;
 	   	
 	   			   else if(roundNumber >= 8 && roundNumber <= 11)
 	   					   roundBonus = 150;
 	   	
 	   				   else
 	   					   roundBonus = 200;
 	   			   }
 	   	
 	   			time =(int) ((Stages.getTimeAllotted()/2) * (GameDriver.game.getBoardGUI().getTurnPercentRemaining()));
 	   	
 	   		//Calculate the correct timeBonus to be given:
 	   			if(time >= 0 && time <= 12)
 	   			{
 	   				timeBonus = 50;
 	   				timeBonus = 0 + (int)(Math.random() * (timeBonus));
 	   		}
 	   	
 	   			if(time >= 13 && time <= 25)
 	   			{
 	   				timeBonus = 100;
 	   				timeBonus = 0 + (int)(Math.random() * (timeBonus));
 	   			}
 	   	
 	   			if(time >= 26 && time <= 37)
 	   			{
 	   				timeBonus = 150;
 	   				timeBonus = 0 + (int)(Math.random() * (timeBonus));
 	   			}
 	   	
 	   			if(time >= 38 && time <= 50)
 	   			{
 	   				timeBonus = 200;
 	   				timeBonus = 0 + (int)(Math.random() * (timeBonus));
 	   			}
 	   	
 	   			int totalMoney = timeBonus + roundBonus;
 	   	
 	   			if(totalMoney >= 250)
 	   				totalMoney = 250;
 	   	
 	   		System.out.println("time is: " + time);
 	   			System.out.println("Round Bonus is: " + roundBonus + " for round " + GameDriver.game.getRoundNumber());
 	   			System.out.println("Time Bonus is: " + timeBonus + " for " + time);
 	   			System.out.println("The player was awarded: " + totalMoney);
 	   			
 	   			
 	   			
 	   		 //Grant the correct end of turn money to the actual correct player
 	   			activePlayer.addMoney(totalMoney);
	}
   
   public void setTimeClass(TimeClass tc){
	   this.tc=tc;
   }
   
   public void setPlayer(Player player) {
		this.player = player;
	}
   
	/**
	 * this method check if the next location is valid
	 * for the player to move
	 * @param i the direction represented by 0-3
	 * @return true if the location is valid and 
	 * player's location isn't going out of townGUI
	 * false otherwise
	 */
	public boolean isValid(int i,int x,int y){
		int x1 = x;
		int y1 = y;
		switch (i){
		case 0:
			if (x>=0)x1 = x1 - speed;
			break;
		case 1:
			if (y>50)y1 = y1 - speed;
			break;
		case 2:
			if (x<=800)x1 = x1 + speed;
			break;
		case 3:
			if (y<460)y1 = y1 + speed;
			break;
		}
		if (x1 <=0 && y1<=240)return false;
		if (x1 >=800 && y1<=240)return false;
		if (x1 >=130 && x1<=190 && y1<=240)return false;
		if (x1 >=325 && x1<=390 && y1<=240)return false;
		if (x1 >=430 && x1<=490 && y1<=240)return false;
		if (x1 >=630 && x1<=690 && y1<=240)return false;
		if (x1 >=130 && x1<=190 && y1>=275)return false;
		if (x1 >=325 && x1<=390 && y1>=275)return false;
		if (x1 >=430 && x1<=490 && y1>=275)return false;
		if (x1 >=630 && x1<=690 && y1>=275)return false;
		return true;
	}
	
	/**
	 * this method move the player to the board
	 * when the player location exit the townGUI
	 */
	public void moveToBoard(int x, int y){
		if (x<0 && y>=245 && y<=275){
			GameDriver.game.boardGUI.removeTownPanel();
			GameDriver.game.boardGUI.startPlayer(350,275,10);
			System.out.println("x is :" + x);
			System.out.println("y is :" + y);
			System.out.println("statement 1");
		}
		else if (y<=50 && x>=350 && x<=450){
			setVisible(false);
			System.out.println("x is :" + x);
			System.out.println("y is :" + y);
			GameDriver.game.boardGUI.removeTownPanel();
			GameDriver.game.boardGUI.startPlayer(425,190,10);
			System.out.println("statement 2");
		}
		else if (x>805 && y>=245 && y<=275){
			setVisible(false);
			System.out.println("x is :" + x);
			System.out.println("y is :" + y);
			GameDriver.game.boardGUI.removeTownPanel();
			GameDriver.game.boardGUI.startPlayer(500,275,10);
			System.out.println("statement 3");
		}
		else if (y>=455 && x>=350 && x<=450){
			setVisible(false);
			System.out.println("x is :" + x);
			System.out.println("y is :" + y);
			GameDriver.game.boardGUI.removeTownPanel();
			GameDriver.game.boardGUI.startPlayer(425,325,10);
			System.out.println("statement 4");
		}
	}
	
}
